package com.block.api.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.block.common.bean.CommonParam;
import com.block.common.exception.ApiException;
import com.block.common.security.util.ApiUtil;
import com.block.common.vo.ResponseVo;
import com.block.datapush.special.service.IDataPushSelfSpecialService;
import com.block.datapush.special.service.ISpecialDataApiInfoService;
import com.block.db.entity.AppEncrypt;
import com.block.db.entity.MemberExtInfo;
import com.block.db.entity.SpecialCostomInfo;
import com.block.db.mapper.MemberExtInfoMapper;
import com.block.db.mapper.SpecialCostomInfoMapper;
import com.block.sms.service.ISmsRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("api/admin")
@Validated
public class AdminController {

    @Autowired
    ISpecialDataApiInfoService specialDataApiInfoService;
    @Autowired
    ISmsRecordService smsRecordService;
    @Autowired
    IDataPushSelfSpecialService dataPushSpecialService2;

    @Resource
    MemberExtInfoMapper memberExtInfoMapper;
    @Resource
    SpecialCostomInfoMapper specialCostomInfoMapper;

    @RequestMapping(value = "/specialPush", method = {RequestMethod.POST, RequestMethod.GET})
    public String specialPush(@RequestAttribute(value = "tranData") JSONObject tranData,
                           @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                           @RequestAttribute(value = "uri") String uri,
                           @RequestAttribute(value = "startDate") Date startdate,
                           @RequestAttribute(value = "ip") String ip) throws ApiException {

        String uid = tranData.getString("uid");
        String infoId = tranData.getString("infoId");

        MemberExtInfo memberExtInfo = memberExtInfoMapper.selectById(uid);
        SpecialCostomInfo costomInfo = specialCostomInfoMapper.selectById(infoId);

        ResponseVo responseVo = dataPushSpecialService2.dataPushSingleCostomInfo(costomInfo, memberExtInfo);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, "1.0", startdate, uri);
    }

    /**
     * 定时任务
     * 查询机构 api 的申请结果
     */
    @RequestMapping(value = "/querySpeApiResult", method = {RequestMethod.POST, RequestMethod.GET})
    public String speApiResult(@RequestAttribute(value = "tranData") JSONObject tranData,
                                     @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                     @RequestAttribute(value = "uri") String uri,
                                     @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                     @RequestAttribute(value = "startDate") Date startdate,
                                     @RequestAttribute(value = "ip") String ip) throws ApiException {

        ResponseVo responseVo = specialDataApiInfoService.queryUpdateApiResult();
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, "1.0", startdate, uri);
    }

}
